package com.atguigu.eud_teacher.controller;
import com.atguigu.commonutils.R;
import com.atguigu.eud_teacher.entity.VideoEntity;
import com.atguigu.eud_teacher.feignclient.CourseFeignVideo;
import com.atguigu.eud_teacher.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * 课程视频
 *
 * @author wuyao
 * @email 13918471632@163.com
 * @date 2021-03-23 12:12:26
 */
@RestController
@RequestMapping("/vue-admin-template/video")
@Api(tags = "小节信息管理接口API")
@CrossOrigin
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private CourseFeignVideo courseFeignVideo;

    //添加小节
    @ApiOperation(value = "添加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody VideoEntity videoEntity) {
        videoService.save(videoEntity);
        return R.ok();
    }

    //根据id获取小节
    @ApiOperation(value = "根据id获取小节")
    @GetMapping("/getVideo/{id}")
    public R getVideo(@PathVariable String id) {
        VideoEntity videoInfo = videoService.getById(id);
        return R.ok().data("videoInfo", videoInfo);
    }

    //修改小节
    @ApiOperation(value = "修改小节")
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody VideoEntity videoEntity) {
        videoService.updateById(videoEntity);
        return R.ok();
    }

    //根据小节删除单个小节
    @ApiOperation(value = "删除小节")
    @DeleteMapping("/deleteVideo/{id}")
    public R deleteVideo(@PathVariable String id) {
        VideoEntity videoEntity = videoService.getById(id);
        String videoId = videoEntity.getVideoSourceId();
        if (!StringUtils.isEmpty(videoId)) {
            courseFeignVideo.deleteVideo(videoEntity.getVideoSourceId());
        }
        videoService.removeById(id);
        return R.ok();
    }

}
